<template>
  <el-row type="flex" style="width: 100%">
    <el-col>{{ tree.name }}</el-col>
    <el-col :span="4">
      <el-row type="flex">
        <el-col>{{ tree.manager }}</el-col>
        <el-col>
          <el-dropdown>
            <span class="el-dropdown-link">
              操作
              <i class="el-icon-arrow-down el-icon--right" />
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="addDep">添加子部门</el-dropdown-item>
              <template v-if="!isRoot">
                <el-dropdown-item @click.native="editDep">编辑</el-dropdown-item>
                <el-dropdown-item
                  @click.native="delNode"
                >删除</el-dropdown-item>
              </template>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { delDepartments } from '@/api/department'
export default {
  name: 'TreeTool',
  props: {
    tree: { type: Object, default: () => {} },
    isRoot: { type: Boolean, default: false }
  },
  methods: {
    async delNode() {
      await this.$confirm('亲亲你真的不要我了吗')
      try {
        await delDepartments(this.tree.id)
      } catch (err) {
        this.$message(err)
        return
      }

      this.$message.success('删除成功')
      this.$emit('updateDeps')
    },
    addDep() {
      this.$emit('addDep', this.tree)
    },
    editDep() {
      this.$emit('editDep', this.tree)
    }
  }
}
</script>

<style></style>
